package org.zuel.huhuForum.controller.interceptor;

import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
@Log4j2
public class WebCorsFilter extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //后端响应基本配置项
        if(request.getMethod().equals("POST")){
            String time=response.getHeader("Access-Control-Max-Age");
            if(time.equals(null)){
                response.setHeader("Access-Control-Allow-Origin", "*");
                response.setHeader("Access-Control-Allow-Methods", "*");
                response.setHeader("Access-Control-Max-Age", "3600");
                response.setHeader("Access-Control-Allow-Headers", "Origin, Authorization,X-Requested-With, Content-Type, Accept, If-Modified-Since，ticket");
                response.setHeader("Access-Control-Request-Headers"," Origin, X-Requested-With, content-Type, Accept, Authorization");

            }
        }
        return true;
    }

}
